<?php
/*------------------------------------------------------------------------
 * User.php 
 *
 * 用户控制器
 * 	
 * Created on 2017-01-16
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2017 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace app\api\controller;

class User extends Base{

	// 修改用户信息
	public function edit_info(){
		$user_id  = $this->user_id();

		$data['realname'] = $this->_post('realname', '姓名不能为空！');
		$data['sex']      = $this->_post('sex', '性别不能为空！');
		$data['mobile']   = $this->_post('mobile', '手机号不能为空！');

		$model = model('User');
		$info = $model->where(array('user_id' => $user_id))->find();
		if($model->mobile_exists($data['mobile']) && $info['mobile'] != $data['mobile']){
			$this->error('手机号已存在，无法修改！');
		}

		$model->where(array('user_id' => $user_id))->update($data);

		$user_info = $model->user_info($user_id);
		$user_info['token'] = $model->get_token($user_id, $this->app_type);

		$this->success('修改成功！', $user_info);
	}
	// 修改密码
	public function edit_password(){
		$user_id     = $this->user_id();
		$newpassword = $this->_post('newpassword', '新密码不能为空！');
		$oldpassword = $this->_post('password', '旧密码不能为空！');

		if(model('User')->change_password($user_id, $oldpassword, $newpassword)){
			$this->success('修改密码成功！');
		}else{
			$this->error(model('User')->tips_info);
		}
	}
	//忘记密码
	public function forget_password(){
		$mobile   = $this->_post('mobile', '手机号不能为空！');
		$password = $this->_post('password', '密码不能为空！');//经过一次MD5加密
		$smscode  = $this->_post('smscode', '验证码不能为空！');

		if(model('Sms')->check_smscode($mobile, $smscode)){
			 $this->error('短信验证码不正确！');
		}

		if(model('User')->forget_password($mobile, $password)){
			$this->success('找回密码成功！');
		}else{
			$this->error(model('User')->tips_info);
		}
	}
	//验证用户密码
	public function check_password(){
		$user_id  = $this->user_id();
		$password = $this->_post('password', '密码不能为空！');

		if(model('User')->check_password($user_id, $password)){
			$this->success('密码验证成功！');
		}else{
			$this->error(model('User')->tips_info);
		}
		
		$this->success('密码验证成功！');
	}
	//更换手机号
	public function change_mobile(){
		$user_id  = $this->user_id();
		$password = $this->_post('password', '密码不能为空！');
		$mobile   = $this->_post('mobile', '新手机号不能为空！');
		$smscode  = $this->_post('smscode', '验证码不能为空！');

		// 验证密码
		if(!model('User')->check_password($user_id, $password)){
			$this->error(model('User')->tips_info);
		}
		// 验证短信码
		if(model('Sms')->check_smscode($mobile, $smscode)){
			 $this->error('短信验证码不正确！');
		}
		// 更新手机号
		if(model('User')->change_mobile($user_id, $mobile)){
			$this->success('手机号更换成功！');
		}else{
			$this->error(model('User')->tips_info);
		}
	}
	//获取用户信息
	public function get_user_info(){
		$user_id = $this->user_id();
		// if(!$user_id){
		// 	$this->error('获取不到用户ID！');
		// }
		//获取用户信息
		$user_info = model('User')->user_info($user_id);
		$user_info['token'] = model('User')->get_token($user_id, $this->app_access_token ? 'app_sso' : '');
		
		$this->success('获取成功！', $user_info);
	}
	//注册获取短信验证码
	public function get_smscode(){
		$mobile = $this->_post('mobile', '手机号不能为空！');
		$type   = $this->_post('type', '', 'register');
		
        if(model('Sms')->send_smscode($mobile, $type)){
            $this->success('发送成功，请注意查收！');
        }else{
            $this->error(model('Sms')->tips_info);
        }
	}
	//获取语音验证码
	public function get_voice_code(){
		$mobile = $this->_post('mobile', '手机号不能为空！');
		$type   = $this->_post('type');
        if(model('Sms')->send_voice_code($mobile, $type)){
            $this->success('发送成功，请注意查收！');
        }else{
            $this->error(model('Sms')->tips_info);
        }
	}
	//检查验证码是否正确
	public function check_smscode(){
		$mobile  = $this->_post('mobile', '手机号不能为空！');
		$smscode = $this->_post('smscode', '验证码不能为空！');
		
		if(model('Sms')->check_smscode($mobile, $smscode)){
			 $this->error('短信验证码不正确！');
		}

		$this->success('验证码正确！');
	}

	//第三方登录
	public function sso_login(){
		$sso_type = $this->_post('sso_type', '第三方类型不能为空！');
		$open_id  = $this->_post('open_id', '第三方用户唯一标识不能为空！');

		//获取绑定用户ID
		$user_id = model('User')->get_sso_user_id($open_id, $sso_type);
		if($user_id){
			$info  = model('User')->user_info($user_id);
			//判断是否禁用
			if($info['status'] == 0){
				$this->error('您的账号被禁用，请联系相关人员！');
			}
			//获取用户登录TOKEN
			$token = model('User')->make_token($user_id);
			$info['token'] = $token;
			$this->success('登录成功！', $info);
		}else{
			$this->error('您的第三方账号尚未绑定！', '', 410);
		}
	}
	//第三方账号绑定
	public function sso_bind(){
		$user_id  = $this->user_id();
		$sso_type = $this->_post('sso_type', '第三方类型不能为空！');
		$open_id  = $this->_post('open_id', '第三方用户唯一标识不能为空！');
		$nickname = $this->_post('nickname');//昵称

		if(model('User')->sso_bind($sso_type, $user_id, $open_id, $nickname)){
            $this->success('绑定成功！');
        }else{
            $this->error(model('User')->tips_info);
        }
	}
	//解除第三方绑定
	public function sso_unbind(){
		$user_id  = $this->user_id();
		$sso_type = $this->_post('sso_type', '第三方类型不能为空！');

		if(model('User')->sso_unbind($sso_type, $user_id)){
            $this->success('取消绑定成功！');
        }else{
            $this->error(model('User')->tips_info);
        }
	}

	// 微信登录二维码
	public function weixin_login_qrcode(){
		$session_id = $this->_get('session_id', '终端用户唯一标识不能为空！');
		// 获取微信推广二维码URL地址
		$params['app_type']   = $this->app_type;
		$params['session_id'] = $session_id;
		$content = model('Weixin')->get_qrcode_url('login', 0, 0, $params, $this->app_type);

		import('QRcode', EXTEND_PATH, '.class.php');
		$file_path = 'uploads/tmp/';
		if(!file_exists($file_path)){
			mkdir($file_path, 755);
		}
		$tmp_file = $file_path . md5(uniqid()) . '.png';
		\QRcode::png($content, $tmp_file, QR_ECLEVEL_L, 20, 0);
		// 获取图片数据
		$file_data = file_get_contents($tmp_file);
		// 删除图片
		unlink($tmp_file);
		$data['scene_id']       = model('Weixin')->scene_id;
		$data['qrcode_content'] = $content;
		$data['qrcode_base64']  = base64_encode($file_data);
		$this->success('获取数据成功！', $data);
		
	}

	// 扫描二维码登录状态
	public function get_scan_qrcode_status(){
		$scene_id = $this->_get('scene_id', '场景ID不能为空！');
		$info     = model('Weixin')->get_scene_id_info($scene_id);
		$user_id  = $info['scan_user_id'];
		if($user_id){
			db('WeixinQrcode')->where(array('scene_id' => $scene_id))->update(array('scan_user_id' => 0));
			// 获取用户信息
			$data = model('User')->user_info($user_id);
			//判断是否禁用
			if($data['status'] == 0){
				$this->error('您的账号被禁用，请联系相关人员！');
			}
			// 更新用户登录TOKEN
			$data['token'] = model('User')->make_token($user_id, $this->app_type, $this->app_id);
			$this->success('登录成功！', $data);
		}else{
			$this->error('用户尚未扫描登录！', '', 203);
		}
	}
}